import { getCompanyInfoTree } from '@/api/company'
import { getSysCompanyBases } from '@/api/system'
import { getNewAlarmInfo } from '@/api/home'
let mixin = {
  created() {
    this.getCompanyInfoTree()
    this.getSysCompanyBases()
    this.setInterval()
  },
  data() {
    return {
      companyArray: [],
      baseArray: [],
      AllalarmList: [],
      typeif: 1,
      timerAll: '',
      baseID: '',
      AllalarmCount: ''
    }
  },
  //销毁钩子
  beforeDestroy() {
    // // 清除定时器
    if (this.timerAll) {
      clearInterval(this.timerAll)
    }
  },
  methods: {
    // 获取公司列表
    getCompanyInfoTree() {
      if (Number(window.localStorage.getItem('userType')) == 0) {
        // 超级管理员，公司只有一个总公司
        getCompanyInfoTree().then(res => {
          if (res.code == 200) {
            this.companyArray = res.data
            this.searchForm.companyId = this.companyArray[0].childList[0].id
            this.getSysCompanyBases()
          }
        })
      } else {
        getCompanyInfoTree({ id: 0 }).then(res => {
          if (res.code == 200) {
            this.companyArray = res.data
            this.searchForm.companyId = this.companyArray[0].id
            this.getSysCompanyBases()
          }
        })
      }
    },
    // 获取基地列表
    getSysCompanyBases() {
      if (this.searchForm.companyId === '' || this.searchForm.companyId == null) {
        return
      }
      getSysCompanyBases({ id: this.searchForm.companyId }).then(res => {
        if (res.code == 200) {
          this.baseArray = res.data
          if (res.data.length != 0) {
            this.searchForm.baseId = this.baseArray[0].id
          }
        }
      })
    },
    // 获取所有报警信息
    getAllalarmList() {
      getNewAlarmInfo({ id: this.searchForm.baseId }).then(res => {
        if (res.code == 200) {
          this.AllalarmList = res.data

          if (this.AllalarmList.time !== null) {
            clearInterval(this.timerAll) //报警直接清除定时器
            const h = this.$createElement
            this.$msgbox({
              title: '报警提醒',
              message: h('p', null, [
                h('div', { class: 'bgc' }),

                h(
                  'span',
                  { class: 'kzx', style: 'color: red;font-size:1.8rem;' },
                  `${this.AllalarmList.boxName ? this.AllalarmList.boxName : ''}！ `
                  // `${this.AllalarmList.content}！`
                ),
                h(
                  'span',
                  { class: 'content', style: 'color: red;font-size:1.8rem;' },
                  `报警内容:${this.AllalarmList.content}！`
                  // `${this.AllalarmList.content}！`
                )
              ]),
              customClass: 'myClass',
              confirmButtonText: '确定'
            })
              .then(() => {
                clearInterval(this.timerAll)
                if (this.AllalarmList.type == 0) {
                  this.$router.push({
                    path: `/home/alarmRecord`
                  })
                }
                if (this.AllalarmList.type == 1) {
                  this.$router.push({
                    path: `/home/serverAlarm`
                  })
                }
                if (this.AllalarmList.type == 2) {
                  this.$router.push({
                    path: `/home/schedulingAlarm`
                  })
                }
              })
              .catch(() => {
                setTimeout(() => {
                  this.setInterval()
                }, 1000000)
              })
          }
        }
      })
    },
    // 3s一次页面刷新
    setInterval() {
      let that = this
      this.timerAll = window.setInterval(() => {
        setTimeout(() => {
          if (this.baseArray.length != 0) {
            that.getAllalarmList()
          } //我们的获取接口数据函数
        }, 0)
      }, 10000)
    }
  }
}
export default mixin
